/*  
 * Copyright (c) 2025 kkaheng
 * SPDX-License-Identifier: MIT  
 */


package com.kkaheng.grid.view.cell;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;

/**
 * 网格单元格基类（对标Android View）
 * 职责：处理背景、边框、padding等通用绘制逻辑
 * @param <T> 单元格数据类型
 */
public abstract class Cell<T> {
    // 样式属性
    private int mBackgroundColor = Color.TRANSPARENT;
    private int mForegroundColor = Color.TRANSPARENT;
    private float mCornerRadius = 0f;
    private float mBorderWidth = 0f;
    private int mBorderColor = Color.TRANSPARENT;
    private final RectF mPadding = new RectF();
    
    // 绘制工具
    protected final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final RectF mTempRect = new RectF();
    
    public Cell() {
        mPaint.setStyle(Paint.Style.FILL);
    }

    /**
     * 核心绘制方法（保持与Android View类似的调用约定）
     * @param data 数据对象
     * @param canvas 画布
     * @param column 列索引
     * @param row 行索引
     * @param width 单元格宽度（像素）
     * @param height 单元格高度（像素）
     * @param scaleFactor 缩放因子（用于适配不同DPI）
     */
    public final void draw(T data, Canvas canvas, int column, int row, 
            float width, float height, float scaleFactor) {
        if (canvas == null || width <= 0 || height <= 0) return;
        float maxWidth = width * scaleFactor;
        float maxHeight = height * scaleFactor;

        // 绘制背景（含圆角）
        drawBackground(canvas, maxWidth, maxHeight, mBackgroundColor);
        
        // 计算内容区域（考虑padding）
        float contentWidth = width - getPaddingHorizontally();
        float contentHeight = height - getPaddingVertically();
        
        if (contentWidth > 0 && contentHeight > 0) {
            canvas.save();
            canvas.translate(mPadding.left * scaleFactor, mPadding.top * scaleFactor);
            canvas.clipRect(0,0,contentWidth * scaleFactor, contentHeight * scaleFactor);
            
            // 调用子类绘制内容
            onDraw(data, canvas, column, row, contentWidth, contentHeight, scaleFactor);
            
            canvas.restore();
        }
        
        drawBackground(canvas, maxWidth, maxHeight, mForegroundColor);
        
        // 绘制边框（在内容之上）
        drawBorder(canvas, maxWidth, maxHeight);
        
    }

    /**
     * 子类实现的内容绘制（对标View.onDraw）
     */
    protected abstract void onDraw(T data, Canvas canvas, int column, int row,
            float width, float height, float scaleFactor);

    /**
     * 测量宽度（含padding）
     */
    public float measureWidth(T data) {
        return getPaddingHorizontally();
    }

    /**
     * 测量高度（含padding）
     */
    public float measureHeight(T data) {
        return getPaddingVertically();
    }

    // ========== 背景/边框绘制 ==========
    private void drawBackground(Canvas canvas, float width, float height, int color) {
        if (Color.alpha(color) == 0) return;
        
        mPaint.setColor(color);
        mPaint.setStyle(Paint.Style.FILL);
        
        if (mCornerRadius > 0) {
            mTempRect.set(0, 0, width, height);
            canvas.drawRoundRect(mTempRect, mCornerRadius, mCornerRadius, mPaint);
        } else {
            canvas.drawRect(0, 0, width, height, mPaint);
        }
    }

    private void drawBorder(Canvas canvas, float width, float height) {
        if (mBorderWidth <= 0 || Color.alpha(mBorderColor) == 0) return;
        
        mPaint.setColor(mBorderColor);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mBorderWidth);
        
        float inset = mBorderWidth / 2;
        mTempRect.set(inset, inset, width - inset, height - inset);
        
        if (mCornerRadius > 0) {
            canvas.drawRoundRect(mTempRect, mCornerRadius, mCornerRadius, mPaint);
        } else {
            canvas.drawRect(mTempRect, mPaint);
        }
        
        mPaint.setStyle(Paint.Style.FILL); // 恢复默认
    }

    // ========== 样式方法 ==========
    public void setBackgroundColor(int color) {
        mBackgroundColor = color;
    }

    public void setForegroundColor(int color) {
    	mForegroundColor = color;
    }

    public void setBorderWidth(float width) {
        mBorderWidth = width;
    }

    public void setBorderColor(int color) {
    	mBorderColor = color;
    }

    public void setCornerRadius(float radius) {
        mCornerRadius = radius;
    }

    public void setPadding(float left, float top, float right, float bottom) {
        mPadding.set(left, top, right, bottom);
    }

    public int getBackgroundColor() {
    	return this.mBackgroundColor;
    }

    public int getForegroundColor() {
    	return this.mForegroundColor;
    }

    public float getPaddingHorizontally() {
        return mPadding.left + mPadding.right;
    }

    public float getPaddingVertically() {
        return mPadding.top + mPadding.bottom;
    }

    public float getPaddingLeft() {
    	return mPadding.left;
    }

    public float getPaddingTop() {
    	return mPadding.top;
    }

    public float getPaddingRight() {
    	return mPadding.right;
    }

    public float getPaddingBottom() {
    	return mPadding.bottom;
    }

    public float getBorderWidth() {
    	return this.mBorderWidth;
    }

    public float getCornerRadius() {
    	return this.mCornerRadius;
    }

    public int getBorderColor() {
    	return this.mBorderColor;
    }

    public Paint getPaint() {
    	return this.mPaint;
    }

}
